//
// Created by wkm on 2024-04-17.
//

#include "xDtime.h"

void xDtime::prc_xDtime() {
    if (reset) {
        next_result = 0;
        next_done = waiting_task;
        x2_reset = true;
        is_two = 0;
    }

    if (start && done == waiting_task) {
        next_result = input_data;
        next_done = preparing_task;
    }

    if (done == preparing_task) {
        x2_reset = false;
        if (is_two == 0) {
            x2_compute_times.write(2);
        } else {
            x2_compute_times.write(3);
        }
        x2_input_data.write(input_data);
        x2_start = true;
        next_done = computing_task;
    }

    if (done == computing_task) {
        if (x2_done == task_finished) {
            is_two = is_two + 1;
            next_result = result.read() ^ x2_result.read();
            if (is_two == 1) {
                next_done = preparing_task;
            } else {
                next_done = task_finished;
            }
            x2_reset = true;
        }
    }

    result = next_result;
    done = next_done;
}